<?php
/**
 * Created by PhpStorm.
 * User: 86182
 * Date: 2020/6/26
 * Time: 13:31
 */

namespace App\Serves;
use App\Models\User;
use App\Utils\Token;
use App\Models\Goods\Goods as GoodsModel;
use App\Models\Goods\GoodsSku as GoodsSkuModel;
use App\Models\Goods\Speci as SpeciModel;
use App\Models\Goods\SpeciValue as SpeciValueModel;

class GoodsServe
{

    public static function getGoodsByGoodsId($goods_id){
        $goods = GoodsModel::getGoodsByGoodsId($goods_id);
        return $goods ;
    }
    public static function getGoodsByCategoryid($catid,$field){
        $goods = GoodsModel::getGoodsByCategoryid($catid,$field);
        return $goods ;
    }

    public static function  getDetails($skuid){

        $skuInfo = GoodsSkuModel::where('id',$skuid)->pluck('goods_id')->toArray();
        $goods_id = $skuInfo[0];
        //获取goodInfo 通过skuid
        $goods = GoodsServe::getGoodsByGoodsId($goods_id);
        $goods['multi_image'] = explode(',', $goods['multi_image']);
        $skusInfo = GoodsSkuModel::where('goods_id',$goods_id)->get()->toArray();
        foreach ($skusInfo as $sku){
            if($sku['id'] == $skuid){
                $active_str = $sku['speci_value_ids'];
            }

        }
//        var_dump($sku);
        $sku_ids = array_column($skusInfo,  'id','speci_value_ids');

        $sku_arr= self::doSkuIds($sku_ids, $active_str);
        $goods['sku'] = $sku_arr;
        return $goods;

    }

    public static function doSkuIds($sku_ids, $active_str){
        $specsValueKeys = array_keys($sku_ids);
        foreach ($specsValueKeys as $specsValueKey){
            $specsValueKey =explode(',', $specsValueKey);
            foreach ($specsValueKey as $k=>$v){
                $specsValueIds[] = $tmp[$k][] = $v;
            }
        }
//        var_dump($specsValueIds);
        $specsValueIds = array_unique($specsValueIds);
        $specsValues = self::getSpecsValuesByIds($specsValueIds);
//       var_dump($specsValues);
        $active_arr = explode(',',$active_str);


        foreach ($tmp as $k=>$v){
            $v = array_unique($v);
//            var_dump($v);
            $data = [];
            foreach ($v as $v1){
                $data[] = [
                    'id'=>$v1,
                    'name'=>$specsValues[$v1]['name'],
                    'active'=>in_array($v1, $active_arr) ? 1 : 0,
                ];
            }
            $res[$k] = [
                'name' => $specsValues[$v[0]]['specs_name'],
                'data' => $data,
            ];
        }
//        var_dump($res);
        return $res;

    }

    public static function getSpecsValuesByIds($ids){
        if(empty($ids)){
            return [];
        }
        try{

        }catch (\Exception $e){
            return [];
        }
        //放到数据库业务层
        $res = SpeciValueModel::whereIn('id',$ids)->get()->toArray();
//        return $res;
        $result=[];
        $specsNames = SpeciModel::get()->toArray();
        $specsNamesArrs = array_column($specsNames, 'name', 'id');
        foreach ($res as $v){
            $result[$v['id']]=[
                'name'=>$v['name'],
                'specs_name'=>$specsNamesArrs[$v['speci_id']] ?? ""
            ];
        }
        return $result;
    }






}